/****************************************************************************
 * Copyright (C) 2009-2010 SciTouch LLC
 * 
 * This file is part of Indigo toolkit.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 3 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ***************************************************************************/

#ifndef __molfile_loader__
#define __molfile_loader__

#include "base_cpp/exception.h"
#include "base_cpp/array.h"
#include "base_cpp/tlscont.h"

class Scanner;
class Molecule;
class GraphHighlighting;
struct Atom;

class MolfileLoader
{
public:
   DEF_ERROR("molfile loader");

   MolfileLoader (Scanner &scanner);

   void loadMolecule (Molecule &mol, bool query);

   // for Rxnfiles v3000
   void loadCtab3000 (Molecule &mol, bool query);

   // optional parameters for reaction
   Array<int> * reaction_atom_mapping;
   Array<int> * reaction_atom_inversion;
   Array<int> * reaction_atom_exact_change;
   Array<int> * reaction_bond_reacting_center;

   // optional highlighting
   GraphHighlighting * highlighting;

   bool ignore_stereocenter_errors;
   bool treat_x_as_pseudoatom; // normally 'X' means 'any halogen'

protected:

   Scanner &_scanner;
   bool     _rgfile;

   TL_CP_DECL(Array<int>, _stereo_care_atoms);
   TL_CP_DECL(Array<int>, _stereocenter_types);
   TL_CP_DECL(Array<int>, _stereocenter_groups);
   TL_CP_DECL(Array<int>, _bond_directions);
   TL_CP_DECL(Array<int>, _ignore_cistrans);

   bool  _v2000;
   int   _atoms_num;
   int   _bonds_num;
   bool  _chiral;

   void _readHeader (Molecule &mol);
   void _readCtabHeader ();
   void _readCtab2000 (Molecule &mol, bool query);
   void _assignCharge (int value, Atom &atom);
   void _read3dFeature2000 (Molecule &mol);
   void _readRGroupOccurenceRanges (const char *str, Array<int> &ranges);
   void _readRGroups2000 (Molecule &mol);
   void _readCtab3000 (Molecule &mol, bool query);
   void _readRGroups3000 (Molecule &query);
   void _preparePseudoAtomLabel (Array<char> &pseudo);
   void _readMultiString (Array<char> &str);
   void _init ();
   void _createStereocenters (Molecule &mol);

   static int _asc_cmp_cb (const int &v1, const int &v2, const void *context);
   void _postLoad (Molecule &mol, bool query);

private:
   MolfileLoader (const MolfileLoader &); // no implicit copy
};

#endif
